SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[SP_TMDOCU_CLPR_Q02]  
/*-------------------------------------------------------*/  
/*---Empresa             : OFISIS S.A.                 --*/  
/*---Cliente             : OFISIS S.A.                 --*/  
/*---Sistema             : Ofiteso        --*/  
/*---M«dulo              : Empresa                     --*/  
/*---Programa            : Pagos y Cobros Proyectados  --*/  
/*---         por dias  y semanas        --*/  
/*---Script              : TMDOCU_CLPR_Q01.sql         --*/  
/*---Nombre SP           : SP_TMDOCU_CLPR_Q01          --*/  
/*---Desarrollado por    : Merardo Antonio Rojas Mendoza-*/  
/*---Fecha Creaci«n      : 07/09/1999                  --*/  
/*---Base Datos          : Microsoft Sql Server        --*/  
/*---Versi«n             : 7.0                         --*/   
/*---Invoca a SP         :                             --*/  
/*-------------------------------------------------------*/  
/*-------------------------------------------------------*/  
/*---Modificado 4 por     : Carlos Marquez E.   --*/  
/*---Fecha Modificaci«n   : 03/12/2004          --*/   
/*---Detalle Modificaci«n : Duplicidad Caso de Letras  --*/  
/*       Estos Docs ya estan incluidos en la Vis--*/  
/*-------------------------------------------------------*/  
/*-------------------------------------------------------*/  
/*---Modificado 2 por     : Denis Villacr's        --*/  
/*---Fecha Modificaci«n   : 14/07/2000         --*/   
/*---Detalle Modificaci«n : Se Agreg½ la variable.     --*/  
/*       @ISTI_FECH          --*/  
/*-------------------------------------------------------*/  
/*-------------------------------------------------------*/  
/*---Modificado 3 por     : Jose Alberto Guevara       --*/  
/*---Fecha Modificaci«n   : 01/04/2003         --*/   
/*---Detalle Modificaci«n : Se Agreg½ las variables     --*/  
/*       @ISCA_WHER_0001,0002       --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/*---Comentarios                                       --*/  
/*---                                                  --*/  
/*---                                                  --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/* DROP PROC SP_TMDOCU_CLPR_Q02     */  
/* GRANT ALL ON SP_TMDOCU_CLPR_Q02 TO PUBLIC     */  
/* EXECUTE SP_TMDOCU_CLPR_Q02 '2004-12-02','DIA','V', 'D', "And TMEMPR.CO_EMPR >= '01' And TMEMPR.CO_EMPR <= '01' And TMAUXI_EMPR.TI_AUXI_EMPR >= 'C' And TMAUXI_EMPR.TI_AUXI_EMPR <= 'C' And TTMONE.CO_MONE >= 'DOL' And TTMONE.CO_MONE <= 'DOL' And TTDOCU_CN
TB.TI_DOCU >= 'FAE' And TTDOCU_CNTB.TI_DOCU <= 'FAE'" ,"" */  
  
@IDFE_VENC  TD_DT_001,  
@ISTI_OPER  TD_VC_003,  
@ISTI_FECH  TD_VC_001,  
@ISTI_REPO TD_VC_001,  
@ISCA_WHER_0001 VARCHAR(1000),  
@ISCA_WHER_0002 VARCHAR(1000)  
 As  
DECLARE  
@VSCO_EMPR_INIC  TD_VC_002,  
@VSCO_EMPR_FINA  TD_VC_002,  
@VSCO_PROV_INIC  TD_VC_020,  
@VSCO_PROV_FINA  TD_VC_020,  
@VSCO_CLIE_INIC  TD_VC_020,  
@VSCO_CLIE_FINA  TD_VC_020,  
@VSCO_MONE_INIC  TD_VC_003,  
@VSCO_MONE_FINA  TD_VC_003,  
@OSTI_DOCU       TD_VC_003,  
@VSDE_FECH_TMDOCP  VarChar(40),  
@VSDE_FECH_TMDOCC  VarChar(40),  
@VSFE_VENC  TD_VC_010,  
@OSTI_ESTA_0001  TD_VC_003,  
@OSTI_ESTA_0007  TD_VC_003   
  
CREATE TABLE #TABLA(CO_EMPR CHAR(2) NULL,  
      CO_TIPO_ENTI CHAR(1) NULL,  
      NO_TIPO_ENTI VARCHAR (15) NULL,  
      CO_ENTI VARCHAR(20) NULL,   
      CO_MONE CHAR(3) NULL,  
       CO_ESTA_DOCU VARCHAR(3) NULL,  
      DE_NOMB VARCHAR(50)  NULL,  
      DE_MONE VARCHAR(50)NULL,   
        NO_AUXI VARCHAR(50) NULL,  
      CO_TIPO_DOCU VARCHAR(3) NULL,  
      NU_DOCU_ENTI VARCHAR(20) NULL,  
   IM_0001 NUMERIC(16,4) NULL,  
   IM_0002 NUMERIC(16,4) NULL,  
   IM_0003 NUMERIC(16,4) NULL,  
   IM_0004 NUMERIC(16,4) NULL,  
   IM_0005 NUMERIC(16,4) NULL,  
   IM_0006 NUMERIC(16,4) NULL,  
   IM_0007 NUMERIC(16,4) NULL,   
   IM_0008 NUMERIC(16,4) NULL,  
   IM_0009 NUMERIC(16,4) NULL,  
   IM_0010 NUMERIC(16,4) NULL,  
   IM_0011 NUMERIC(16,4) NULL,  
   IM_0012 NUMERIC(16,4) NULL,  
   IM_0013 NUMERIC(16,4) NULL,  
   IM_0014 NUMERIC(16,4) NULL,  
   IM_0015 NUMERIC(16,4) NULL,  
   IM_SALD NUMERIC(16,4) NULL)  
  
    /* Devuelve el Estado de Documento 'ACT' */  
     Exec   SP_TTTIES_DOCU_Q01 1, @OSTI_ESTA_0001 Output   
    /* Devuelve el Estado de Documento 'ACE'  */  
    Exec   SP_TTTIES_DOCU_Q01 7, @OSTI_ESTA_0007 Output   
  
  
  
  SELECT @ISCA_WHER_0001= REPLACE(@ISCA_WHER_0001,'TMEMPR','T1')  
  SELECT @ISCA_WHER_0001= REPLACE(@ISCA_WHER_0001,'TMAUXI_EMPR','T3')  
  SELECT @ISCA_WHER_0001= REPLACE(@ISCA_WHER_0001,'TTDOCU_CNTB','T5')  
  SELECT @ISCA_WHER_0001= REPLACE(@ISCA_WHER_0001,'TTMONE','T2')  
  
  SELECT @ISCA_WHER_0002= REPLACE(@ISCA_WHER_0002,'TMEMPR','T1')  
  SELECT @ISCA_WHER_0002= REPLACE(@ISCA_WHER_0002,'TMAUXI_EMPR','T3')  
  SELECT @ISCA_WHER_0002= REPLACE(@ISCA_WHER_0002,'TTDOCU_CNTB','T5')  
  SELECT @ISCA_WHER_0002= REPLACE(@ISCA_WHER_0002,'TTMONE','T2')  
  
/* SELECCION POR FECHA DE VENCIMIENTO */  
  
IF @ISTI_FECH = 'V'   
SELECT @VSDE_FECH_TMDOCP = 'TMDOCP_GENE.FE_VENC'  
SELECT @VSDE_FECH_TMDOCC = 'TMDOCC_GENE.FE_VENC'  
  
IF @ISTI_FECH = 'P'   
SELECT @VSDE_FECH_TMDOCP = 'TMDOCP_GENE.FE_PROG_PAGO'  
  
  
  
SELECT @VSFE_VENC = Convert(VarChar(10),@IDFE_VENC,103)  
  
  
EXEC SP_TTDOCU_CNTB_Q02 6,@OSTI_DOCU    
  
IF @ISTI_REPO='D'  
BEGIN  
 IF @ISTI_OPER = 'DIA'  
  BEGIN  
  EXECUTE (' INSERT INTO #TABLA (CO_EMPR , CO_MONE, CO_TIPO_ENTI, NO_TIPO_ENTI, CO_ENTI, CO_ESTA_DOCU, DE_NOMB, DE_MONE, NO_AUXI, CO_TIPO_DOCU, NU_DOCU_ENTI,  
         IM_0001, IM_0002, IM_0003, IM_0004, IM_0005, IM_0006,   
         IM_0007, IM_0008, IM_0009, IM_0010, IM_0011, IM_0012,  
         IM_0013, IM_0014, IM_0015 )  
   SELECT   
    TMDOCP_GENE.CO_EMPR ,  
    TMDOCP_GENE.CO_MONE ,  
    "'+'P'+'",  
    "'+'PROVEEDOR'+'",  
    TMDOCP_GENE.CO_PROV ,  
    TMDOCP_GENE.CO_ESTA_DOCU,  
    T1.DE_NOMB,   
    T2.DE_MONE,  
    T3.NO_AUXI,  
    TMDOCP_GENE.CO_TIPO_DOCU,  
    TMDOCP_GENE.NU_DOCU_PROV,  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))  
   FROM    TMDOCP_GENE  ,TMEMPR T1, TTMONE T2 , TMAUXI_EMPR T3 , TTDOCU_CNTB T5  
   WHERE  TMDOCP_GENE.CO_ESTA_DOCU = "'+@OSTI_ESTA_0001+'"        
     ' +@ISCA_WHER_0001+@ISCA_WHER_0002+'     
     And   '+@VSDE_FECH_TMDOCP+'  >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And   '+@VSDE_FECH_TMDOCP+'  < Dateadd(day,16,Convert(DateTime,"'+@VSFE_VENC+'",103))  
       And   T1.CO_EMPR   = TMDOCP_GENE.CO_EMPR  
       And   TMDOCP_GENE.CO_MONE  = T2.CO_MONE  
       And   TMDOCP_GENE.CO_EMPR  = T3.CO_EMPR  
     And   T3.TI_AUXI_EMPR = "P"  
       And   T3.CO_AUXI_EMPR =  TMDOCP_GENE.CO_PROV  
       And   TMDOCP_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
    
     
  UNION  
    
  SELECT   
    TMDOCC_GENE.CO_EMPR,  
    TMDOCC_GENE.CO_MONE,  
    "'+'C'+'",  
    "'+'CLIENTE'+'",  
    TMDOCC_GENE.CO_CLIE,  
    TMDOCC_GENE.CO_ESTA_DOCU,  
    T1.DE_NOMB,   T2.DE_MONE,  
    T3.NO_AUXI,  
   TMDOCC_GENE.CO_TIPO_DOCU,  
   TMDOCC_GENE.NU_DOCU_CLIE,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "10")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "11")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "12")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "13")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "14")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))   
     from    TMDOCC_GENE  ,TMEMPR T1 , TTMONE T2, TMAUXI_EMPR T3, TTDOCU_CNTB T5  
     WHERE  '+@VSDE_FECH_TMDOCC+'  >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And   '+@VSDE_FECH_TMDOCC+'  < Dateadd(day,16,Convert(DateTime,"'+@VSFE_VENC+'",103))  
   '+@ISCA_WHER_0001+@ISCA_WHER_0002+'     
       And   T1.CO_EMPR    =  TMDOCC_GENE.CO_EMPR  
       And   TMDOCC_GENE.CO_MONE   =  T2.CO_MONE  
       And   TMDOCC_GENE.CO_EMPR   =  T3.CO_EMPR  
     and     T3.TI_AUXI_EMPR = "C"  
       And   T3.CO_AUXI_EMPR   =  TMDOCC_GENE.CO_CLIE  
       And   TMDOCC_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
      
    
   ORDER BY  1 , 2, 3')  
 END   
  
/*OPCION DE SEMANAS*/  
  
 IF @ISTI_OPER = 'SEM'  
 BEGIN   
   
  EXECUTE (' INSERT INTO #TABLA (CO_EMPR , CO_MONE, CO_TIPO_ENTI, NO_TIPO_ENTI, CO_ENTI, CO_ESTA_DOCU, DE_NOMB, DE_MONE, NO_AUXI, CO_TIPO_DOCU, NU_DOCU_ENTI,  
         IM_0001, IM_0002, IM_0003, IM_0004, IM_0005, IM_0006,   
         IM_0007, IM_0008, IM_0009, IM_0010, IM_0011, IM_0012,  
         IM_0013, IM_0014, IM_0015 )  
   SELECT    
    TMDOCP_GENE.CO_EMPR ,  
    TMDOCP_GENE.CO_MONE ,  
    "'+'P'+'",  
    "'+'PROVEEDOR'+'",  
    TMDOCP_GENE.CO_PROV ,  
    TMDOCP_GENE.CO_ESTA_DOCU,  
    T1.DE_NOMB,  
    T2.DE_MONE,  
    T3.NO_AUXI ,  
     TMDOCP_GENE.CO_TIPO_DOCU,  
                 TMDOCP_GENE.NU_DOCU_PROV,  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")),  
    PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))  
   From  TMDOCP_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3  ,  TTDOCU_CNTB T5  
   WHERE     '+@VSDE_FECH_TMDOCP+' >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And  '+@VSDE_FECH_TMDOCP+' < dateadd(day,119,Convert(DateTime,"'+@VSFE_VENC+'",103))  
     '+@ISCA_WHER_0001+@ISCA_WHER_0002+'     
       And   TMDOCP_GENE.CO_ESTA_DOCU =  "'+@OSTI_ESTA_0001+'"   
       And   T1.CO_EMPR  =  TMDOCP_GENE.CO_EMPR  
       And   TMDOCP_GENE.CO_MONE =  T2.CO_MONE  
       And   TMDOCP_GENE.CO_EMPR =  T3.CO_EMPR  
       And   T3.TI_AUXI_EMPR = "P"  
       And   T3.CO_AUXI_EMPR =  TMDOCP_GENE.CO_PROV  
       And   TMDOCP_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
    
    
  UNION  
    
  SELECT    
   TMDOCC_GENE.CO_EMPR ,  
   TMDOCC_GENE.CO_MONE ,  
   "'+'C'+'", "'+'CLIENTE'+'",  
   TMDOCC_GENE.CO_CLIE ,  
   TMDOCC_GENE.CO_ESTA_DOCU,  
   T1.DE_NOMB,  
   T2.DE_MONE,  
   T3.NO_AUXI ,  
  TMDOCC_GENE.CO_TIPO_DOCU,  
  TMDOCC_GENE.NU_DOCU_CLIE,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ,  
     PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))   
   From  TMDOCC_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3 , TTDOCU_CNTB T5  
   WHERE  '+@VSDE_FECH_TMDOCC+' >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And  '+@VSDE_FECH_TMDOCC+' < dateadd(day,119,Convert(DateTime,"'+@VSFE_VENC+'",103))  
   '+@ISCA_WHER_0001+@ISCA_WHER_0002+'     
     And  T1.CO_EMPR  =  TMDOCC_GENE.CO_EMPR  
     And  TMDOCC_GENE.CO_MONE =  T2.CO_MONE  
     And  TMDOCC_GENE.CO_EMPR =  T3.CO_EMPR  
   and    T3.TI_AUXI_EMPR = "C"  
     And  T3.CO_AUXI_EMPR =  TMDOCC_GENE.CO_CLIE  
     And   TMDOCC_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
    
    
  ORDER BY 1, 2, 3')   
   
  END  
  
END  
  
ELSE  
BEGIN  
 IF @ISTI_OPER = 'DIA'  
  BEGIN  
  EXECUTE (' INSERT INTO #TABLA (CO_EMPR , CO_MONE, CO_TIPO_ENTI, NO_TIPO_ENTI, CO_ENTI, CO_ESTA_DOCU, DE_NOMB, DE_MONE, NO_AUXI,  
         IM_0001, IM_0002, IM_0003, IM_0004, IM_0005, IM_0006,   
         IM_0007, IM_0008, IM_0009, IM_0010, IM_0011, IM_0012,  
         IM_0013, IM_0014, IM_0015 )  
  SELECT   
    TMDOCP_GENE.CO_EMPR ,  
    TMDOCP_GENE.CO_MONE ,  
    "'+'P'+'",  
    "'+'PROVEEDOR'+'",  
    TMDOCP_GENE.CO_PROV ,  
    MAX(TMDOCP_GENE.CO_ESTA_DOCU),  
    MAX(T1.DE_NOMB),   
    MAX(T2.DE_MONE),  
    MAX(T3.NO_AUXI),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')),  "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) )  
   FROM    TMDOCP_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3 , TTDOCU_CNTB T5  
   WHERE   '+@VSDE_FECH_TMDOCP+'  >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And   '+@VSDE_FECH_TMDOCP+'  < Dateadd(day,16,Convert(DateTime,"'+@VSFE_VENC+'",103))  
     '+@ISCA_WHER_0001+@ISCA_WHER_0002+'     
     And   TMDOCP_GENE.CO_ESTA_DOCU = "'+@OSTI_ESTA_0001+'"    
       And   T1.CO_EMPR   = TMDOCP_GENE.CO_EMPR  
       And   TMDOCP_GENE.CO_MONE  = T2.CO_MONE  
       And   TMDOCP_GENE.CO_EMPR  = T3.CO_EMPR  
     And   T3.TI_AUXI_EMPR = "P"  
       And   T3.CO_AUXI_EMPR =  TMDOCP_GENE.CO_PROV  
       And   TMDOCP_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
    
  GROUP BY TMDOCP_GENE.CO_EMPR, TMDOCP_GENE.CO_MONE, TMDOCP_GENE.CO_PROV   
  UNION   
     
  SELECT  
      TMDOCP_GENE.CO_EMPR,  
    TMDOCP_GENE.CO_MONE,  
    "'+'P'+'",  
    "'+'PROVEEDOR'+'",  
     TMDOCP_GENE.CO_PROV,  
     MAX(TMDOCP_GENE.CO_ESTA_DOCU),  
     MAX(T1.DE_NOMB),  
    MAX(T2.DE_MONE),  
     MAX(T3.NO_AUXI),  
     SUM( PATINDEX( CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) ))  
   FROM  TMDOCP_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3, TTDOCU_CNTB T5  
    WHERE TMDOCP_GENE.FE_VENC >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And  TMDOCP_GENE.FE_VENC <  Dateadd(Day,16,Convert(DateTime,"'+@VSFE_VENC+'",103))  
   '+@ISCA_WHER_0001+@ISCA_WHER_0002+'     
     And  TMDOCP_GENE.CO_ESTA_DOCU =  "'+@OSTI_ESTA_0007+'"   
       And  T1.CO_EMPR  =  TMDOCP_GENE.CO_EMPR  
       And  TMDOCP_GENE.CO_MONE =  T2.CO_MONE  
       And  TMDOCP_GENE.CO_EMPR =  T3.CO_EMPR  
     And  T3.TI_AUXI_EMPR =  "P"  
       And  T3.CO_AUXI_EMPR =  TMDOCP_GENE.CO_PROV  
   GROUP BY TMDOCP_GENE.CO_EMPR, TMDOCP_GENE.CO_MONE, TMDOCP_GENE.CO_PROV  
    
  UNION  
    
  SELECT   
    TMDOCC_GENE.CO_EMPR,  
    TMDOCC_GENE.CO_MONE,  
    "'+'C'+'",  
    "'+'CLIENTE'+'",  
    TMDOCC_GENE.CO_CLIE,  
    MAX(TMDOCC_GENE.CO_ESTA_DOCU),  
    MAX(T1.DE_NOMB),   MAX(T2.DE_MONE),  
    MAX(T3.NO_AUXI),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')),  "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "10")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "11")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "12")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "13")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "14")*( ISNULL( IM_TOTA, 0) -ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) )  
   from    TMDOCC_GENE  ,TMEMPR T1 , TTMONE T2, TMAUXI_EMPR T3, TTDOCU_CNTB T5  
   WHERE   '+@VSDE_FECH_TMDOCC+'  >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And   '+@VSDE_FECH_TMDOCC+'  < Dateadd(day,16,Convert(DateTime,"'+@VSFE_VENC+'",103))  
  '+@ISCA_WHER_0001+@ISCA_WHER_0002+'     
       And   T1.CO_EMPR    =  TMDOCC_GENE.CO_EMPR  
       And   TMDOCC_GENE.CO_MONE   =  T2.CO_MONE  
       And   TMDOCC_GENE.CO_EMPR   =  T3.CO_EMPR  
     and   T3.TI_AUXI_EMPR = "C"  
       And   T3.CO_AUXI_EMPR   =  TMDOCC_GENE.CO_CLIE  
       And   TMDOCC_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
    GROUP BY TMDOCC_GENE.CO_EMPR, TMDOCC_GENE.CO_MONE, TMDOCC_GENE.CO_CLIE   
    
  UNION   
    
  SELECT  
    TMDOCC_GENE.CO_EMPR ,  
   TMDOCC_GENE.CO_MONE ,  
    "'+'C'+'",  
    "'+'CLIENTE'+'",  
    TMDOCC_GENE.CO_CLIE ,  
    MAX(TMDOCC_GENE.CO_ESTA_DOCU),  
    MAX(T1.DE_NOMB),  
    MAX(T2.DE_MONE),  
    MAX(T3.NO_AUXI),  
     SUM( PATINDEX( CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( DAY, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) ))  
      from  TMDOCC_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3,TTDOCU_CNTB T5  
     WHERE TMDOCC_GENE.FE_VENC >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And  TMDOCC_GENE.FE_VENC <  Dateadd(Day,16,Convert(DateTime,"'+@VSFE_VENC+'",103))  
   '+@ISCA_WHER_0001+@ISCA_WHER_0002+'     
     And  T1.CO_EMPR  =  TMDOCC_GENE.CO_EMPR  
     And  TMDOCC_GENE.CO_MONE =  T2.CO_MONE  
     And  TMDOCC_GENE.CO_EMPR =  T3.CO_EMPR  
   and    T3.TI_AUXI_EMPR = "C"  
     And  T3.CO_AUXI_EMPR =  TMDOCC_GENE.CO_CLIE  
   GROUP BY TMDOCC_GENE.CO_EMPR, TMDOCC_GENE.CO_MONE, TMDOCC_GENE.CO_CLIE   
   ORDER BY  1 , 2, 3')  
   
 END   
   
 /*OPCION DE SEMANAS*/  
   
 IF @ISTI_OPER = 'SEM'  
 BEGIN   
   
  EXECUTE (' INSERT INTO #TABLA (CO_EMPR , CO_MONE, CO_TIPO_ENTI, NO_TIPO_ENTI, CO_ENTI, CO_ESTA_DOCU, DE_NOMB, DE_MONE, NO_AUXI,  
         IM_0001, IM_0002, IM_0003, IM_0004, IM_0005, IM_0006,   
         IM_0007, IM_0008, IM_0009, IM_0010, IM_0011, IM_0012,  
         IM_0013, IM_0014, IM_0015 )  
   SELECT    
    TMDOCP_GENE.CO_EMPR ,  
    TMDOCP_GENE.CO_MONE ,  
    "'+'P'+'",  
    "'+'PROVEEDOR'+'",  
    TMDOCP_GENE.CO_PROV ,  
    MAX(TMDOCP_GENE.CO_ESTA_DOCU),  
    MAX(T1.DE_NOMB),  
    MAX(T2.DE_MONE),  
    MAX(T3.NO_AUXI) ,  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N"))),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCP+')), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")))  
   From  TMDOCP_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3  , TTDOCU_CNTB T5  
   WHERE   '+@VSDE_FECH_TMDOCP+' >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And  '+@VSDE_FECH_TMDOCP+' < dateadd(day,119,Convert(DateTime,"'+@VSFE_VENC+'",103))  
   '+@ISCA_WHER_0001+@ISCA_WHER_0002+'       
     And    TMDOCP_GENE.CO_ESTA_DOCU =  "'+@OSTI_ESTA_0001+'"   
       And  T1.CO_EMPR  =  TMDOCP_GENE.CO_EMPR  
       And  TMDOCP_GENE.CO_MONE =  T2.CO_MONE  
       And  TMDOCP_GENE.CO_EMPR =  T3.CO_EMPR  
     And    T3.TI_AUXI_EMPR = "P"  
       And  T3.CO_AUXI_EMPR =  TMDOCP_GENE.CO_PROV  
       And   TMDOCP_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
    
  GROUP BY TMDOCP_GENE.CO_EMPR, TMDOCP_GENE.CO_MONE, TMDOCP_GENE.CO_PROV  
    
  UNION   
    
  SELECT     
   TMDOCP_GENE.CO_EMPR ,   
   TMDOCP_GENE.CO_MONE ,  
    "'+'P'+'",  
    "'+'PROVEEDOR'+'",  
   TMDOCP_GENE.CO_PROV ,  
   MAX(TMDOCP_GENE.CO_ESTA_DOCU),  
   MAX(T1.DE_NOMB),  
   MAX(T2.DE_MONE),  
   MAX(T3.NO_AUXI),   
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCP_GENE.FE_VENC)), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) ))  
   From TMDOCP_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3, TTDOCU_CNTB T5  
   WHERE   TMDOCP_GENE.FE_VENC >= Convert(DateTime,"'+@VSFE_VENC+'",103)    
   and  TMDOCP_GENE.FE_VENC <  dateadd(day,119,Convert(DateTime,"'+@VSFE_VENC+'",103))  
   '+@ISCA_WHER_0001 + @ISCA_WHER_0002+'    
   and    TMDOCP_GENE.CO_ESTA_DOCU =  "'+@OSTI_ESTA_0007+'"   
     And  T1.CO_EMPR  =  TMDOCP_GENE.CO_EMPR  
     And  TMDOCP_GENE.CO_MONE =  T2.CO_MONE  
     And  TMDOCP_GENE.CO_EMPR =  T3.CO_EMPR  
   and    T3.TI_AUXI_EMPR = "P"  
     And  T3.CO_AUXI_EMPR =  TMDOCP_GENE.CO_PROV  
  GROUP BY TMDOCP_GENE.CO_EMPR, TMDOCP_GENE.CO_MONE, TMDOCP_GENE.CO_PROV  
    
  UNION  
    
  SELECT    
   TMDOCC_GENE.CO_EMPR ,  
   TMDOCC_GENE.CO_MONE ,  
    "'+'C'+'",  
    "'+'CLIENTE'+'",  
   TMDOCC_GENE.CO_CLIE ,  
   MAX(TMDOCC_GENE.CO_ESTA_DOCU),  
   MAX(T1.DE_NOMB),  
   MAX(T2.DE_MONE),  
   MAX(T3.NO_AUXI) ,  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) ),  
    SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), '+@VSDE_FECH_TMDOCC+')), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )*(PATINDEX(T5.ST_SIGN, "S") - PATINDEX(T5.ST_SIGN, "N")) )  
   From  TMDOCC_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3 ,  TTDOCU_CNTB T5  
   WHERE  '+@VSDE_FECH_TMDOCC+' >= Convert(DateTime,"'+@VSFE_VENC+'",103)  
     And  '+@VSDE_FECH_TMDOCC+' < dateadd(day,119,Convert(DateTime,"'+@VSFE_VENC+'",103))  
    '+@ISCA_WHER_0001+@ISCA_WHER_0002+'    
     And  T1.CO_EMPR  =  TMDOCC_GENE.CO_EMPR  
     And  TMDOCC_GENE.CO_MONE =  T2.CO_MONE  
     And  TMDOCC_GENE.CO_EMPR =  T3.CO_EMPR  
   and    T3.TI_AUXI_EMPR = "C"  
     And  T3.CO_AUXI_EMPR =  TMDOCC_GENE.CO_CLIE  
     And   TMDOCC_GENE.CO_TIPO_DOCU = T5.TI_DOCU  
   GROUP BY TMDOCC_GENE.CO_EMPR, TMDOCC_GENE.CO_MONE, TMDOCC_GENE.CO_CLIE  
    
  UNION   
    
  SELECT     
   TMDOCC_GENE.CO_EMPR ,  
   TMDOCC_GENE.CO_MONE ,  
    "'+'C'+'",  
    "'+'CLIENTE'+'",  
   TMDOCC_GENE.CO_CLIE ,  
   MAX(TMDOCC_GENE.CO_ESTA_DOCU),  
   MAX(T1.DE_NOMB),  
   MAX(T2.DE_MONE),  
   MAX(T3.NO_AUXI),   
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "0")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "1")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "2")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "3")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "4")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "5")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "6")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "7")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "8")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "9")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "10")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "11")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "12")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "13")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) )),  
   SUM( PATINDEX(CONVERT(VARCHAR(2), DATEDIFF ( WEEK, Convert(DateTime,"'+@VSFE_VENC+'",103), TMDOCC_GENE.FE_VENC)), "14")*( ISNULL( IM_TOTA, 0) - ISNULL( IM_PAGA,0) ))  
   From TMDOCC_GENE  ,TMEMPR T1 , TTMONE T2 , TMAUXI_EMPR T3, TTDOCU_CNTB T5  
   WHERE  TMDOCC_GENE.FE_VENC >= Convert(DateTime,"'+@VSFE_VENC+'",103)    
   and  TMDOCC_GENE.FE_VENC <  dateadd(day,119,Convert(DateTime,"'+@VSFE_VENC+'",103))  
   '+@ISCA_WHER_0001+@ISCA_WHER_0002+'    
     And  T1.CO_EMPR  =  TMDOCC_GENE.CO_EMPR  
     And  TMDOCC_GENE.CO_MONE =  T2.CO_MONE  
     And  TMDOCC_GENE.CO_EMPR =  T3.CO_EMPR  
   and    T3.TI_AUXI_EMPR = "C"  
     And  T3.CO_AUXI_EMPR =  TMDOCC_GENE.CO_CLIE     
  GROUP BY TMDOCC_GENE.CO_EMPR, TMDOCC_GENE.CO_MONE, TMDOCC_GENE.CO_CLIE  
  ORDER BY 1, 2, 3')   
 END  
END  
 EXECUTE ('Insert into #TABLA (CO_EMPR,  CO_TIPO_ENTI, CO_ENTI , CO_MONE , CO_ESTA_DOCU ,  
            DE_NOMB , DE_MONE , NO_AUXI , IM_0001 ,IM_0002 ,  
                 IM_0003 , IM_0004 , IM_0005 ,  
                 IM_0006 , IM_0007 , IM_0008 ,  
                 IM_0009 , IM_0010 , IM_0011 ,  
                 IM_0012 , IM_0013 , IM_0014 ,  
                 IM_0015 , IM_SALD)  
 Select CO_EMPR,  "'+'P'+'", CO_PROV, CO_MONE, NULL, NULL, NULL, NULL, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, Sum(IM_TOTA-IM_PAGA)  
 From   TMDOCP_GENE  
 Where  FE_VENC <  Convert(DateTime,"'+@VSFE_VENC+'",103)  
   And  CO_ESTA_DOCU =  "'+@OSTI_ESTA_0001+'"   
 Group By     CO_EMPR, CO_MONE, CO_PROV  
   
 UNION  
   
 Select CO_EMPR, "'+'C'+'", CO_CLIE, CO_MONE, NULL, NULL, NULL, NULL, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, Sum(IM_TOTA-IM_PAGA)  
 From TMDOCC_GENE   
 Where   FE_VENC <  Convert(DateTime,"'+@VSFE_VENC+'",103)  
 and     CO_ESTA_DOCU =  "'+@OSTI_ESTA_0001+'"   
 Group By     CO_EMPR, CO_MONE, CO_CLIE')  
   
   
 Update #TABLA  
 Set  
 IM_SALD = IM_SALD* (-1),  
 IM_0001 = IM_0001 * (-1),  
 IM_0002 = IM_0002 * (-1),  
 IM_0003 = IM_0003 * (-1),  
 IM_0004 =  IM_0004 * (-1),  
 IM_0005 =  IM_0005 * (-1),  
 IM_0006 =  IM_0006 * (-1),  
 IM_0007 =  IM_0007 * (-1),  
 IM_0008  = IM_0008 * (-1),  
 IM_0009 =  IM_0009 * (-1),  
 IM_0010 =  IM_0010 * (-1),  
 IM_0011 =  IM_0011 * (-1),  
 IM_0012 =  IM_0012 * (-1),  
 IM_0013 =  IM_0013 * (-1),  
 IM_0014 = IM_0014 * (-1),  
 IM_0015 = IM_0015 * (-1)  
 Where CO_TIPO_ENTI =  'P'  
  
IF @ISTI_REPO='D'  
BEGIN  
SELECT CO_EMPR, CO_MONE, CO_TIPO_ENTI, CO_ENTI, MAX(CO_ESTA_DOCU), MAX(DE_NOMB),MAX(DE_MONE), MAX(NO_TIPO_ENTI),  
  MAX(NO_AUXI),   
             SUM(ISNULL( IM_0001, 0) ), SUM(ISNULL( IM_0002, 0) ),  
 SUM(ISNULL( IM_0003, 0) ), SUM(ISNULL( IM_0004, 0) ),  
 SUM(ISNULL( IM_0005, 0) ), SUM(ISNULL( IM_0006, 0) ),  
 SUM(ISNULL( IM_0007, 0) ), SUM(ISNULL( IM_0008, 0) ),  
 SUM(ISNULL( IM_0009, 0) ), SUM(ISNULL( IM_0010, 0) ),  
 SUM(ISNULL( IM_0011, 0) ), SUM(ISNULL( IM_0012, 0) ),  
 SUM(ISNULL( IM_0013, 0) ), SUM(ISNULL( IM_0014, 0) ),  
 SUM(ISNULL( IM_0015, 0) ), SUM(ISNULL( IM_SALD, 0) ), CO_TIPO_DOCU, NU_DOCU_ENTI  
FROM #TABLA   
GROUP BY CO_EMPR , CO_MONE, CO_TIPO_ENTI, CO_ENTI, CO_TIPO_DOCU, NU_DOCU_ENTI  
HAVING (  SUM(ISNULL( IM_0001, 0) )<>0  OR    SUM(ISNULL( IM_0002, 0) )<>0   OR  
                 SUM(ISNULL( IM_0003, 0) )<>0   OR    SUM(ISNULL( IM_0004, 0) )<>0   OR  
     SUM(ISNULL( IM_0005, 0) )<>0   OR    SUM(ISNULL( IM_0006, 0) )<>0   OR  
     SUM(ISNULL( IM_0007, 0) )<>0   OR    SUM(ISNULL( IM_0008, 0) )<>0   OR  
     SUM(ISNULL( IM_0009, 0) )<>0   OR    SUM(ISNULL( IM_0010, 0) )<>0   OR  
     SUM(ISNULL( IM_0011, 0) )<>0   OR    SUM(ISNULL( IM_0012, 0) )<>0   OR  
     SUM(ISNULL( IM_0013, 0) )<>0   OR    SUM(ISNULL( IM_0014, 0) )<>0   OR  
     SUM(ISNULL( IM_0015, 0) )<>0  )  
ORDER BY CO_EMPR , CO_MONE, CO_TIPO_ENTI, CO_ENTI, CO_TIPO_DOCU, NU_DOCU_ENTI  
END  
  
ELSE  
BEGIN  
SELECT CO_EMPR, CO_MONE, CO_TIPO_ENTI, CO_ENTI, MAX(CO_ESTA_DOCU), MAX(DE_NOMB),MAX(DE_MONE), MAX(NO_TIPO_ENTI),  
  MAX(NO_AUXI), SUM(ISNULL( IM_0001, 0) ), SUM(ISNULL( IM_0002, 0) ),  
 SUM(ISNULL( IM_0003, 0) ), SUM(ISNULL( IM_0004, 0) ),  
 SUM(ISNULL( IM_0005, 0) ), SUM(ISNULL( IM_0006, 0) ),  
 SUM(ISNULL( IM_0007, 0) ), SUM(ISNULL( IM_0008, 0) ),  
 SUM(ISNULL( IM_0009, 0) ), SUM(ISNULL( IM_0010, 0) ),  
 SUM(ISNULL( IM_0011, 0) ), SUM(ISNULL( IM_0012, 0) ),  
 SUM(ISNULL( IM_0013, 0) ), SUM(ISNULL( IM_0014, 0) ),  
 SUM(ISNULL( IM_0015, 0) ), SUM(ISNULL( IM_SALD, 0) ), NULL, NULL  
FROM #TABLA   
GROUP BY CO_EMPR , CO_MONE, CO_TIPO_ENTI, CO_ENTI  
HAVING (  SUM(ISNULL( IM_0001, 0) )<>0  OR    SUM(ISNULL( IM_0002, 0) )<>0   OR  
                 SUM(ISNULL( IM_0003, 0) )<>0   OR    SUM(ISNULL( IM_0004, 0) )<>0   OR  
     SUM(ISNULL( IM_0005, 0) )<>0   OR    SUM(ISNULL( IM_0006, 0) )<>0   OR  
     SUM(ISNULL( IM_0007, 0) )<>0   OR    SUM(ISNULL( IM_0008, 0) )<>0   OR  
     SUM(ISNULL( IM_0009, 0) )<>0   OR SUM(ISNULL( IM_0010, 0) )<>0   OR  
     SUM(ISNULL( IM_0011, 0) )<>0   OR    SUM(ISNULL( IM_0012, 0) )<>0   OR  
     SUM(ISNULL( IM_0013, 0) )<>0   OR    SUM(ISNULL( IM_0014, 0) )<>0   OR  
     SUM(ISNULL( IM_0015, 0) )<>0  )  
ORDER BY CO_EMPR , CO_MONE, CO_TIPO_ENTI, CO_ENTI  
END  
/*------------------------- FIN ------------------------------ */


GO
